﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.ConditionExtract.Common;
using Ledict.Utility.Extension;

namespace Ledict.ConditionExtract.DAL
{
   public class ArgumentTypeDAL
    {
         private string DbName;
         public ArgumentTypeDAL()
        {
            DbName = "JudgeDoc";
        }
         /// <summary>
         /// 是否存在该记录
         /// 案由内编号不重复且
         /// </summary>
         public string Exists(SYS_ArgumentType Model)
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append(@"
             select  AT_ID  from SYS_ArgumentType
             where (AT_CauseID=@AT_CauseID or isnull(AT_CauseID,'')='')
             and AT_Code=@AT_Code
");
             Database oBase = DatabaseFactory.CreateDatabase(DbName);
             DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
             oBase.AddInParameter(oCmd, "@AT_CauseID", DbType.String, Model.AT_CauseID);
             oBase.AddInParameter(oCmd, "@AT_Code", DbType.String, Model.AT_Code);
             return LdFormat.ObjectToString (oBase.ExecuteScalar(oCmd));
         }
        
         public decimal  GetAT_ShowIndex()
         {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("select max(AT_ShowIndex)+1 from SYS_ArgumentType ");
             Database oBase = DatabaseFactory.CreateDatabase(DbName);
             DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
             return Convert.ToDecimal (oBase.ExecuteScalar(oCmd));
             
         }

        /// <summary>
        /// 获取字典编号
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public string GetArgumentTypeNumber(string ccId,string ccName, string argumentName)
        {
           string name, str1;
            name=str1= PinYinConverter.GetFirst(ccName);
            if (argumentName == "诉讼地位")
            {
                str1 = str1 + "001";
            } else if (argumentName == "货币单位")
            {
                str1 = str1 + "002";
            } else
            {
                string sql="select top 1 AT_Code  from dbo.SYS_ArgumentType where AT_CauseID='" + ccId + "'order by AT_Code desc ";
                Database oBase1 = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd1 = oBase1.GetSqlStringCommand(sql.ToString());
                string st= Convert.ToString(oBase1.ExecuteScalar(oCmd1));
                if (!string.IsNullOrEmpty(st))
                {
                    str1 = str1 + (Convert.ToString(Convert.ToInt32(st.Replace(name, "")) + 1001).Remove(0, 1));
                } else
                {
                    str1 = str1 + "001";
                }                
            }
            return str1;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
       public bool Add(SYS_ArgumentType Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"insert into SYS_ArgumentType(AT_ID,AT_Name,AT_Code,AT_ShowIndex,AT_MatchKeyWord,AT_CauseID)
            values 	(@AT_ID,@AT_Name,@AT_Code,@AT_ShowIndex,@AT_MatchKeyWord,@AT_CauseID)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@AT_ID", DbType.String, Model.AT_ID);
            oBase.AddInParameter(oCmd, "@AT_Name", DbType.String, Model.AT_Name);
            oBase.AddInParameter(oCmd, "@AT_Code", DbType.String, Model.AT_Code);
            oBase.AddInParameter(oCmd, "@AT_ShowIndex", DbType.Decimal, Model.AT_ShowIndex);
            oBase.AddInParameter(oCmd, "@AT_MatchKeyWord", DbType.String, Model.AT_MatchKeyWord);
            oBase.AddInParameter(oCmd, "@AT_CauseID", DbType.String, Model.AT_CauseID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
       public bool Delete(string AT_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SYS_ArgumentType ");
            strSql.Append(" where AT_ID=@AT_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@AT_ID", DbType.String, AT_ID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteSysArgumentType(string AT_CauseID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SYS_ArgumentType ");
            strSql.Append(" where AT_CauseID=@AT_CauseID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@AT_CauseID", DbType.String, AT_CauseID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        public bool Update(SYS_ArgumentType Model)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(@" update SYS_ArgumentType set               
           AT_Name=@AT_Name,                   
           AT_Code=@AT_Code,                   
           AT_MatchKeyWord=@AT_MatchKeyWord,   
           AT_CauseID=@AT_CauseID              
           where AT_ID=@AT_ID                 
       ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@AT_ID", DbType.String, Model.AT_ID);
            oBase.AddInParameter(oCmd, "@AT_Name", DbType.String, Model.AT_Name);
            oBase.AddInParameter(oCmd, "@AT_Code", DbType.String, Model.AT_Code);
            oBase.AddInParameter(oCmd, "@AT_MatchKeyWord", DbType.String, Model.AT_MatchKeyWord);
            oBase.AddInParameter(oCmd, "@AT_CauseID", DbType.String, Model.AT_CauseID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;

        }

        /// <summary>
       /// 获得数据列表(AT_MatchKeyWord遇到;|；换行用来方便看，但数据库中不存换行 )
        /// </summary>
       public DataTable GetList(string AT_CauseID, string SelectArgument)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select row_number()over(order by  AT_ShowIndex  ) as ArgumentShowIndex ,
            *
            FROM SYS_ArgumentType 
            where isnull(AT_CauseID,'')='" + @AT_CauseID+
            "' and (AT_Name like'%" + @SelectArgument + "%'or AT_Code like '%" + @SelectArgument + "%' or AT_MatchKeyWord like '%" + @SelectArgument + "%')");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@AT_CauseID", DbType.String , AT_CauseID);
                 oBase.AddInParameter(oCmd, "@SelectArgument", DbType.String , SelectArgument);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

       public SYS_ArgumentType GetMatchKeyWord(string AT_Code)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(@" select top 1
            *
           from dbo.SYS_ArgumentType
           where AT_Code=@AT_Code");

           Database oBase = DatabaseFactory.CreateDatabase(DbName);
           DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
           oBase.AddInParameter(oCmd, "@AT_Code", DbType.String, AT_Code);
           DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
           if (OtB.Rows.Count > 0)
           {
               return DataRowToModel(OtB.Rows[0]);
           }
           else
           {
               return new SYS_ArgumentType();
           }
       }

       public SYS_ArgumentType Get_AT_MatchKeyWord(string AT_CauseID)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(@"  select * from SYS_ArgumentType where AT_CauseID=@AT_CauseID and AT_Name = '诉讼请求种类'");
           Database oBase = DatabaseFactory.CreateDatabase(DbName);
           DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
           oBase.AddInParameter(oCmd, "@AT_CauseID",DbType.String, AT_CauseID);
           DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
           if (OtB.Rows.Count > 0)
           {
               return DataRowToModel(OtB.Rows[0]);
           }
           else
           {
               return null;
           }
       }


       /// <summary>
       /// 得到一个对象实体
       /// </summary>
       public SYS_ArgumentType DataRowToModel(DataRow row)
       {
           SYS_ArgumentType model = new SYS_ArgumentType();
           if (row != null)
           {
               if (row["AT_ID"] != null)
               {
                   model.AT_ID = row["AT_ID"].ToString();
               }
               if (row["AT_Name"] != null)
               {
                   model.AT_Name = row["AT_Name"].ToString();
               }
               if (row["AT_Code"] != null)
               {
                   model.AT_Code = row["AT_Code"].ToString();
               }
               if (row["AT_ShowIndex"] != null && row["AT_ShowIndex"].ToString() != "")
               {
                   model.AT_ShowIndex = decimal.Parse(row["AT_ShowIndex"].ToString());
               }
              
               if (row["AT_MatchKeyWord"] != null)
               {
                   model.AT_MatchKeyWord = row["AT_MatchKeyWord"].ToString();
               }
               if (row["AT_CauseID"] != null)
               {
                   model.AT_CauseID = row["AT_CauseID"].ToString();
               }
           }
           return model;
       }

        /// <summary>
        /// 获取诉讼地位字典值
        /// </summary>
        /// <returns></returns>
        public string GetSSDWKeyWord()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 AT_MatchKeyWord from dbo.SYS_ArgumentType where at_name='诉讼地位'and AT_RelatCaseType is null ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return Convert.ToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 获取货币单位字典值
        /// </summary>
        /// <returns></returns>
        public string GetHBDWKeyWord()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 AT_MatchKeyWord from dbo.SYS_ArgumentType where at_name='货币单位'and AT_RelatCaseType<>'Common'   ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return Convert.ToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 获取是否有字典值
        /// </summary>
        /// <param name="atName"></param>
        /// <param name="atCauseId"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public string GetZiDianZhi(string atName, string atCauseId, string keyword)
        {
            string strSql = string.Format("select AT_Code from SYS_ArgumentType where AT_MatchKeyWord = '{0}' and AT_CauseID = '{1}' and AT_Name = '{2}'",keyword,atCauseId,atName);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return Convert.ToString(oBase.ExecuteScalar(oCmd));
        }
        //select AT_Code from SYS_ArgumentType where AT_MatchKeyWord = '' and AT_CauseID = '' and AT_Name = ''
    }
}
